//
//  HomeTopupPOPView.swift
//  NDAi
//
//  Created by 丸子 on 2024/6/4.
//

import UIKit

class HomeTopupPOPView: UIView {
    var chooseClose:( () -> Void)?
    var chooseDone:(() -> Void)?
    
    lazy var backIV:UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage(named: "icon_top_tips")
        return imageView
    }()
    
    lazy var titleLb: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hexString: "#3F2266")
        label.font = UIFont.pingFangSCSemibold(ofSize: 20)
        label.text = "您还暂未开通会员"
        label.textAlignment = .center
        return label
    }()
    
    lazy var descLb: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hexString: "#928892")
        label.font = UIFont.pingFangSCMedium(ofSize: 14)
        label.textAlignment = .center
        label.numberOfLines = 2
        return label
    }()
    
    lazy var bottomBtn: UIButton = {
        let button = UIButton(type: .custom)
        button.titleLabel?.font = UIFont.pingFangSCMedium(ofSize: 14)
        button.setTitle("立即开通", for: .normal)
        button.setTitleColor(.white, for: .normal)
        button.allRound(18)
        button.backgroundColor = UIColor(hexString: "#A05FFF")
        return button
    }()
    
    lazy var bottomLb: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.red
        label.font = UIFont.pingFangSCMedium(ofSize: 14)
        label.text = "免费次数已用完"
        label.textAlignment = .center
        label.numberOfLines = 2
        return label
    }()
    
    lazy var closeBtn: UIButton = {
        let button = UIButton(type: .custom)
        button.imageForNormal = UIImage(named: "icon_vip_close")
        return button
    }()
    
    override init(frame: CGRect) {
        super.init(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT))
        backgroundColor = UIColor(hexString: "#000000", transparency: 0.3)
        addSubviews([backIV, closeBtn, titleLb, descLb, bottomBtn, bottomLb])
        backIV.snp.makeConstraints { make in
            make.centerX.centerY.equalToSuperview()
            make.width.equalTo(280)
            make.height.equalTo(310)
        }
        
        closeBtn.snp.makeConstraints { make in
            make.top.equalTo(backIV.snp.top).offset(6)
            make.trailing.equalTo(backIV.snp.trailing).offset(-6)
            make.width.height.equalTo(24)
        }
        
        bottomBtn.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.width.equalTo(148)
            make.height.equalTo(36)
            make.bottom.equalTo(bottomLb.snp.top).offset(-10)
        }
        
        bottomLb.snp.makeConstraints { make in
            //            make.top.equalTo(bottomBtn.snp.bottom).offset(10)
            make.bottom.equalTo(backIV.snp.bottom).offset(-15)
            make.centerX.equalToSuperview()
        }
        
        descLb.snp.makeConstraints { make in
            make.bottom.equalTo(bottomBtn.snp.top).offset(-16)
            make.leading.trailing.equalTo(backIV).inset(30)
        }
        
        titleLb.snp.makeConstraints { make in
            make.bottom.equalTo(descLb.snp.top).offset(-10)
            make.leading.trailing.equalToSuperview().inset(15)
        }
        
        bottomBtn.setClosureTouchUpInside_k { [weak self] in
            self?.removeFromSuperview()
            self?.chooseDone?()
        }
        
        
        closeBtn.setClosureTouchUpInside_k { [weak self] in
            self?.removeFromSuperview()
            self?.chooseClose?()
        }
        
        
        
        // 创建文本
              let fullText = "低至0.01元每天，畅享无限对话，不限制次数"

              // 创建一个可变的属性字符串
              let attributedString = NSMutableAttributedString(string: fullText)

              // 设定红色的属性
              let redTextAttributes: [NSAttributedString.Key: Any] = [
                  .foregroundColor: UIColor.red
              ]

              // 查找 "0.01" 的范围
              if let range = fullText.range(of: "0.01") {
                  let nsRange = NSRange(range, in: fullText)
                  // 将 "0.01" 设置为红色
                  attributedString.addAttributes(redTextAttributes, range: nsRange)
              }

              // 设置 label 的 attributedText
              descLb.attributedText = attributedString
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
}

class HomeTwoTopupPOPView: UIView {
    var chooseClose:( () -> Void)?
    var chooseDone:(() -> Void)?
    lazy var backIV:UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage(named: "icon_top_tips")
        return imageView
    }()
    
    lazy var titleLb: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hexString: "#3F2266")
        label.font = UIFont.pingFangSCSemibold(ofSize: 20)
        label.text = "是否要放弃订阅会员"
        label.textAlignment = .center
        return label
    }()
    
    lazy var descLb: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hexString: "#928892")
        label.font = UIFont.pingFangSCMedium(ofSize: 14)
        label.text = "优惠力度低至 0.01元/天"
        label.textAlignment = .center
        label.numberOfLines = 2
        return label
    }()
    
    lazy var bottomBtn: UIButton = {
        let button = UIButton(type: .custom)
        button.titleLabel?.font = UIFont.pingFangSCMedium(ofSize: 14)
        button.setTitle("立即开通", for: .normal)
        button.setTitleColor(.white, for: .normal)
        button.allRound(18)
        button.backgroundColor = UIColor(hexString: "#A05FFF")
        return button
    }()
    
    lazy var cancelBtn: UIButton = {
        let button = UIButton(type: .custom)
        button.titleLabel?.font = UIFont.pingFangSCMedium(ofSize: 14)
        button.setTitle("狠心放弃", for: .normal)
        button.setTitleColor(UIColor(hexString: "#A765FF"), for: .normal)
        button.allRound(18)
        button.layer.borderWidth = 1
        button.layer.borderColor = UIColor(hexString: "#A05FFF").cgColor
        return button
    }()
    
    lazy var bottomLb: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.red
        label.font = UIFont.pingFangSCMedium(ofSize: 14)
        label.text = "倒计时："
        label.textAlignment = .center
        label.numberOfLines = 2
        return label
    }()
    
    
    lazy var closeBtn: UIButton = {
        let button = UIButton(type: .custom)
        button.imageForNormal = UIImage(named: "icon_vip_close")
        return button
    }()
    var timer: Timer?
    
    override init(frame: CGRect) {
        super.init(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT))
        backgroundColor = UIColor(hexString: "#000000", transparency: 0.3)
        addSubviews([backIV, closeBtn, titleLb, descLb, cancelBtn, bottomBtn, bottomLb])
        backIV.snp.makeConstraints { make in
            make.centerX.centerY.equalToSuperview()
            make.width.equalTo(280)
            make.height.equalTo(310)
        }
        
        closeBtn.snp.makeConstraints { make in
            make.top.equalTo(backIV.snp.top).offset(6)
            make.trailing.equalTo(backIV.snp.trailing).offset(-6)
            make.width.height.equalTo(24)
        }
        
        cancelBtn.snp.makeConstraints { make in
            make.leading.equalTo(backIV.snp.leading).offset(16)
            make.bottom.equalTo(bottomLb.snp.top).offset(-10)
            make.height.equalTo(36)
            make.width.equalTo(bottomBtn.snp.width)
        }
        
        bottomBtn.snp.makeConstraints { make in
            make.trailing.equalTo(backIV.snp.trailing).offset(-16)
            make.bottom.equalTo(bottomLb.snp.top).offset(-10)
            make.height.equalTo(36)
            make.leading.equalTo(cancelBtn.snp.trailing).offset(10)
        }
        
        bottomLb.snp.makeConstraints { make in
            make.bottom.equalTo(backIV.snp.bottom).offset(-15)
            make.centerX.equalToSuperview()
        }
        
        descLb.snp.makeConstraints { make in
            make.bottom.equalTo(bottomBtn.snp.top).offset(-16)
            make.leading.trailing.equalTo(backIV).inset(30)
        }
        
        titleLb.snp.makeConstraints { make in
            make.bottom.equalTo(descLb.snp.top).offset(-10)
            make.leading.trailing.equalToSuperview().inset(15)
        }
        
        bottomBtn.setClosureTouchUpInside_k { [weak self] in
            self?.timer?.invalidate()
            self?.removeFromSuperview()
            self?.chooseDone?()
        }
        
        closeBtn.setClosureTouchUpInside_k { [weak self] in
            self?.timer?.invalidate()
            self?.removeFromSuperview()
        }
        
        cancelBtn.setClosureTouchUpInside_k { [weak self] in
            self?.timer?.invalidate()
            self?.removeFromSuperview()
            self?.chooseClose?()
        }
        startCountdown()
        
        // 创建文本
              let fullText = "优惠力度低至 0.01元/天"

              // 创建一个可变的属性字符串
              let attributedString = NSMutableAttributedString(string: fullText)

              // 设定红色的属性
              let redTextAttributes: [NSAttributedString.Key: Any] = [
                  .foregroundColor: UIColor.red
              ]

              // 查找 "0.01" 的范围
              if let range = fullText.range(of: "0.01") {
                  let nsRange = NSRange(range, in: fullText)
                  // 将 "0.01" 设置为红色
                  attributedString.addAttributes(redTextAttributes, range: nsRange)
              }

              // 设置 label 的 attributedText
              descLb.attributedText = attributedString
        
    }
    func startCountdown() {
        updateLabel()
        timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(updateLabel), userInfo: nil, repeats: true)
    }
    
    @objc func updateLabel() {
        let currentTime = Date()
        let calendar = Calendar.current
        let nextMidnight = calendar.nextDate(after: currentTime, matching: DateComponents(hour: 0, minute: 0, second: 0), matchingPolicy: .nextTime)!
        
        let remainingTime = nextMidnight.timeIntervalSince(currentTime)
        let hours = Int(remainingTime) / 3600
        let minutes = (Int(remainingTime) % 3600) / 60
        let seconds = Int(remainingTime) % 60
        
        bottomLb.text = String(format: "倒计时: %02d:%02d:%02d", hours, minutes, seconds)
        
        if remainingTime <= 0 {
            timer?.invalidate()
            bottomLb.text = "倒计时已结束"
        }
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    deinit {
        timer?.invalidate()
        
    }
    
}
